package com.hq.service;

import com.hq.bean.Student;
import com.hq.dao.StuDAO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StuServiceImpl implements StuService {

    @Resource
    private StuDAO stuDAO;

    @Override
    public List list(Map map) {
        return stuDAO.list(map);
    }

    @Override
    public List courseList() {
        return stuDAO.courseList();
    }

    @Override
    public List cityList(Integer pid) {
        return stuDAO.cityList(pid);
    }

    @Override
    public int addStu(Student student, Integer[] cids) {
        // 多对多的添加1：学生添加 添加同时获取主键id
        int i=stuDAO.addStu(student);
        // 添加中间表
        Map map=new HashMap();
        map.put("sid",student.getSid());
        map.put("cids",cids);
        stuDAO.addMi(map);
        return i;
    }

    @Override
    public int dels(String ids) {
        // 删除中间表
        stuDAO.delsMi(ids);
        int i = stuDAO.delsStu(ids);
        return i;
    }

    @Override
    public Map getInfoById(Integer sid) {
        return stuDAO.getInfoById(sid);
    }

    @Override
    public int upd(Student student, Integer[] cids) {
        //1:修改学生表
        int i=stuDAO.upd(student);
        //2:删除中间表
        stuDAO.delsMi(student.getSid()+"");
        //3:添加中间表
        Map map=new HashMap();
        map.put("sid",student.getSid());
        map.put("cids",cids);
        stuDAO.addMi(map);
        return i;
    }
}
